<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class
 *
 * @author ww
 */
class StepThreeStrategy extends AStepStrategy {

    public function init() {
        $context = $this->getContext();
        $form_manager = $context->getCurrentController()->getFormManager();
        
        if ($typeoftask = $form_manager->getSessionVariable('task_type')) {
            $this->typeoftask = $typeoftask;
        } else {
            $context->setStatus(NOT_FOUND);
            return;
        }
        if (!TaskFormHelper::isAllowedStep(TaskFormHelper::STEP3_FORM_NAME, $typeoftask)) {
            $context->setStatus(NOT_FOUND);
            return;
        }
               
    }

    public function getHTML() {
        $context = $this->context;
        $form_manager = $context->getCurrentController()->getFormManager();
        $view = $context->getCurrentController()->getView();
     
        $task_form_factory = new TaskFormFactoryThree();
        $task_form = $task_form_factory->createTaskForm($this->typeoftask); 
        $previous_data = loadDataFromSession(TaskFormHelper::STEP3_FORM_NAME. '_' . $this->typeoftask . '_validation');
        $errors = $form_manager->getErrorsForFields('create_task');       
        $params = array('data' => $previous_data, 'errors' => $errors);
        
        $form_body_html = $task_form->getFormHTML($params, $view);
        $buttons = '<a href="' . HTTP_SERVER . 'task/add/step2' . '">Назад</a><input type="submit" value="Следующий шаг" name="nextstep">';
        return $form_manager->build_form($form_body_html, 'create_task', $buttons);
    }

    public function validate_form(&$data, &$scope) {
        $context = $this->getContext();
        $manager = $context->getCurrentController()->getFormManager();
        
        $typeoftask = $manager->getSessionVariable('task_type'); 
        if (isset(TaskFormHelper::$allowedtypes[$typeoftask])) {
            
            $manager->saveDataInToSession($data, TaskFormHelper::STEP3_FORM_NAME . '_' . $typeoftask . '_validation');
            
            $task_form_factory = new TaskFormFactoryThree();
            
            $task_form = $task_form_factory->createTaskForm($typeoftask); 
            $validations_res = $task_form->validate_form($data, $scope, $manager);
            
            if (in_array(VALIDATION_FAILED, $validations_res)) {
                return VALIDATION_FAILED;
            }
            else
                return VALIDATION_ACCEPTED;
        }
        else {
            Event::fireEvent('message', array('message' => "Попытка взлома?"));
            return VALIDATION_FAILED;
        }  
    }

    public function submit_form($data, $scope) {
        $context = $this->getContext();
        $manager = $context->getCurrentController()->getFormManager();
        $typeoftask = $manager->getSessionVariable('task_type');

        $task_form_factory = new TaskFormFactoryThree();

        $task_form = $task_form_factory->createTaskForm($typeoftask);
        $task_form->submit_form($data, $scope, $manager);
    }

}
?>
